import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;

public abstract class MySet <Item> implements Iterable<Item>{

    int size;

    protected MySet(){}

    @Override
    public abstract Iterator<Item> iterator();

    public abstract void addElement(Item item);

    public abstract boolean contains(Object o);

    public  MySet<Item> unionWith(MySet<? extends Item> s){
        MySet<Item> newSet = null;
        try {
            newSet = this.getClass().getDeclaredConstructor().newInstance();
        }
        catch (Exception e){
            e.getStackTrace();
        }
        for (Item item : this) {
            newSet.addElement(item);
        }
        for (Item item : s) {
            newSet.addElement(item);
        }
        return newSet;
    }

    public MySet<Item> intersectionWith(MySet<? extends Item> s){
        MySet<Item> newSet = null;
        try {
            newSet = this.getClass().getDeclaredConstructor().newInstance();
        }
        catch (Exception e){
            e.getStackTrace();
        }
        for (Item item : s) {
            if(this.contains(item)){
                newSet.addElement(item);
            }
        }
        return newSet;
    }

    public boolean isEmpty(){
        return size==0;
    }

    public int size(){
        return size;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Item item : this) {
            sb.append(item+" ");
        }
        return sb.toString();
    }
}
